#include "Collision.h"

Collision::Collision()
{
	m_Veclocity_vx = m_Veclocity_vy = 0.0f;
}

DIR Collision::isCollision()
{
	float normalX, normalY;
	float timeCollision;

	// vat nam trong khong gian cua doi tuong 
	if (CheckAABB(GetSweptBroadPhaseBox(m_Character), m_Object) != 0)
	{
		// kiem tra va cham khong can biet van toc cua doi tuong
		if (AABB(m_Character, m_Object) == DIR::NONE_DIR)
		{
			timeCollision = SweptAABB(m_Character, m_Object, normalX, normalY);

			if (timeCollision > 0.0f && timeCollision < 1.0f)
			{
				if (abs(m_Veclocity_vx) >= abs(m_Character.vx * timeCollision + normalX))
				{
					m_Veclocity_vx = m_Character.vx * timeCollision + normalX;
				}

				if (abs(m_Veclocity_vy) >= abs(m_Character.vy * timeCollision + normalY))
				{
					m_Veclocity_vy = m_Character.vy * timeCollision + normalY;
				}

				if (normalX != 0.0f)
				{
					if (normalX == -1.0f)
						return DIR::LEFT;

					if (normalX == 1.0f)
						return DIR::RIGHT;
				}
				else
				{
					if (normalY != 0.0f)
					{
						if (normalY == 1.0f)
							return DIR::TOP;

						if (normalY == -1.0f)
							return DIR::BOTTOM;
					}
				}
			}
		}
		else
		{
			return (AABB(m_Character, m_Object));
		}
	}
	else
	{
		return DIR::NONE_DIR;
	}
}

void Collision::Init(Box _Animation, Box _Object)
{
	m_Character = _Animation;
	m_Veclocity_vx = m_Character.vx;
	m_Veclocity_vy = m_Character.vy;
	m_Object = _Object;
}